/**
 * 
 */
package com.base.myproject.client.busi.service;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;

/**
 * @author zgw 2009-10-20 下午03:16:12 javazgw@gmail.com
 */
public class AssociatorPanel extends ContentPanel implements Listener<BaseEvent>,AsyncCallback<Associator>{

	
	TextField<String> associator_no_t ;

	TextField<String> associator_name_t;

	TextField<String> associator_type_t ;

	TextField<String> associator_jifen_t;

	TextField<String> associator_finance_t;
	
	TextField<String> associator_phone_t;
	public AssociatorPanel() {
		init();
	}

	void init() {
		this.setHeaderVisible(false);
		this.setHeight("200");
		TableLayout tl = new TableLayout(4);
		tl.setWidth("600");
		setLayout(tl);
		TableData td=new TableData();
		td.setColspan(1);
	//	td.setWidth("2000");
		td.setMargin(100);
		td.setPadding(5);
		td.setRowspan(1);
		Html html = new Html();
		html
				.setHtml("<a style=\"font-size:12px;color: #FF0000\"><strong>输入  </strong></a> <a style=\"text-decoration:underline;font-size:12px;color: #FF0000\"> 会员名、</a>" +
						" <a style=\"text-decoration:underline;font-size:12px;color: #FF0000\"> 会员号、</a>" +
						"  <a style=\"text-decoration:underline;font-size:12px;color: #FF0000\"> 电话</a> " +
						"<a style=\"font-size:12px;color: #FF0000\"><strong> 可以找到会员信息。使用刷卡机的时候请将光标移动到 </strong></a>" +
						"<a style=\"text-decoration:underline;font-size:12px;color: #FF0000\"> 会员号</a>");
		
		
		Label associator_no = new Label("会员号:");
		associator_no_t = new TextField<String>();
		Label associator_name = new Label("会员名:");
		associator_name_t = new TextField<String>();
		 
		Label associator_phone = new Label("电话:");
		associator_phone_t = new TextField<String>();
		Label associator_no_type = new Label("会员类型:");
		associator_type_t = new TextField<String>();
		Label associator_jifen = new Label("会员积分:");
		associator_jifen_t = new TextField<String>();
		Label associator_finance = new Label("资金余额:");
		 associator_finance_t = new TextField<String>();
		 associator_finance_t.setEnabled(false);
		 associator_jifen_t.setEnabled(false);
		 associator_type_t.setEnabled(false);
		//Button b = new Button();
		 TableData td2=new TableData();
		 
		 td2.setColspan(4);
		 this.add(html,td2);
		
		this.add(associator_no,td);
		this.add(associator_no_t,td);
		this.add(associator_name,td);
		this.add(associator_name_t,td);
		this.add(associator_phone,td);
		this.add(associator_phone_t,td);
		this.add(associator_no_type,td);
		this.add(associator_type_t,td);
		this.add(associator_jifen,td);
		this.add(associator_jifen_t,td);
		this.add(associator_finance,td);
		this.add(associator_finance_t,td);
		//this.add(b,td);
//		this.add(b);
		//b.addListener(Events.Select, this);
//		b.addSelectionListener(new SelectionListener<ButtonEvent>() {
//
//			@Override
//			public void componentSelected(ButtonEvent ce) {
//				// TODO Auto-generated method stub
//				final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
//						.create(com.base.myproject.client.tools.GreetingService.class);
//				greetingService.getAssociator("1003", 
//						new AsyncCallback<Associator>() {
//							public void onFailure(Throwable caught) {
//								// Show the RPC error message to the user
//								System.out.println("error");
//							}
//
//							public void onSuccess(Associator result) {
//								System.out.println(result.getFinance());
//
//							}
//						});
//
//			}
//
//		});
		

		associator_no_t.addListener(Events.Change, this);
		associator_name_t.addListener(Events.Change, this);
		associator_phone_t.addListener(Events.Change, this);
	}

	@Override
	public void onFailure(Throwable caught) {
		
		System.out.println("error");
	}

	@Override
	public void onSuccess(Associator result) {
		
		if(result.getName()!=null)
		{
		associator_no_t.setValue(result.getCcode());
		associator_name_t.setValue(result.getName());
		associator_phone_t.setValue(result.getPhone());
		associator_type_t.setValue(result.getType());
		associator_jifen_t.setValue(""+result.getJifen());
		associator_finance_t.setValue(""+result.getFinance());
		}
		else
		{
			Info.display("提示", "没有找到会员", result.getCcode());  
			reset();
		}
	}

	@Override
	public void handleEvent(BaseEvent be) {
		if(be.getType().equals(Events.Select))
		{
			/*final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
			.create(com.base.myproject.client.tools.GreetingService.class);
			greetingService.getAssociator("1003", this);*/
		}
		else if(be.getType().equals(Events.Change))
		{
			
			if(be.getSource().equals(associator_no_t))
			{
				final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
				greetingService.getAssociator(associator_no_t.getValue(),1,this);
			}
			else if(be.getSource().equals(associator_name_t))
			{
				final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
				greetingService.getAssociator(associator_name_t.getValue(),2,this);
			}
			else if(be.getSource().equals(associator_phone_t))
			{
				final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
				greetingService.getAssociator(associator_phone_t.getValue(),3,this);
			}
				
			
			
		}
			
		
	}
	
	Dialog getDialog()
	{
		Dialog dialog = new Dialog();
		dialog.setSize(600, 200);
		dialog.add(this);	
		
		return dialog;	
		
	}
	
	/**
	 * 重置
	 */
	void reset()
	{
		associator_no_t.setValue("");
		associator_name_t.setValue("");
		associator_phone_t.setValue("");
		associator_type_t.setValue("");
		associator_jifen_t.setValue("");
		associator_finance_t.setValue("");
	}
	/**
	 * 会员号
	 * @return
	 */
	public String getCcode()
	{
		return associator_no_t.getValue();
	}
	/**
	 * 会员名
	 * @return
	 */
	public String getName()
	{
		return associator_name_t.getValue();
	}
	/**
	 * 电话
	 * @return
	 */
	public String getPhone()
	{
		return associator_phone_t.getValue();
	}
	/**
	 * 资金余额
	 * @return
	 */
	public String getFinance()
	{
		return associator_finance_t.getValue();
	}
	/**
	 * 会员类型
	 * @return
	 */
	public String getType()
	{
		return associator_type_t.getValue();
	}
	/**
	 * 会员积分 
	 * @return
	 */
	public String getJifen()
	{
		return associator_jifen_t.getValue();
	}
}
